//
// CrackApp
//
// Copyright (C) jextra.net.
//
//  This file is part of the Crack build system.
//
//  The Crack build system is free software; you can redistribute it 
//  and/or modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  The Crack build system is distributed in the hope that it will be 
//  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with the Crack build system; if not, write to the Free
//  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
//  02111-1307 USA.
//

package net.jextra.crack.core;

import java.util.*;
import net.jextra.crack.*;

/**
 * <p>
 * Base class for the Crack and CrackAdmin.
 * Simply contains some conventions plus it creates the important {@link CrackClassLoader}.
 * </p>
 */
public abstract class CrackApp
{
    // ============================================================
    // Fields
    // ============================================================

    public static final String CMD_HELP = "help";
    public static final String PRIMARY_HEADER = "================================================================================";
    public static final String PRIMARY_HEADER_FORMAT = "= %-76s =\n";

    // ============================================================
    // Constructors
    // ============================================================

    public CrackApp()
        throws Exception
    {
    }

    // ============================================================
    // Methods
    // ============================================================

    // ----------
    // protected
    // ----------

    /**
     * Verify that the CRACK_HOME is a directory.
     */
    protected void verifyCrackHome()
    {
        if ( CrackContext.getInstance().getCrackHome() == null || !CrackContext.getInstance().getCrackHome().exists()
            || !CrackContext.getInstance().getCrackHome().isDirectory() )
        {
            throw new CrackError( "CRACK_HOME property does not point to a directory: '%s'.", CrackContext
                .getInstance().getCrackHome() );
        }
    }

    protected void printCrackVersion()
        throws Exception
    {
        System.out.printf( "Crack version %s\n", CrackContext.getInstance().getCrackVersion() );
        System.out.println();
        System.out.println( "Plugin versions:" );
        System.out.println();
        Map<String, String> versions = CrackContext.getInstance().getPluginVersions();

        int maxLength = 0;
        ArrayList<String> names = new ArrayList<String>();
        for ( String name : versions.keySet() )
        {
            names.add( name );
            if ( name.length() > maxLength )
            {
                maxLength = name.length();
            }
        }

        Collections.sort( names );

        for ( String name : names )
        {
            System.out.printf( "  %-" + maxLength + "s  %s\n", name, versions.get( name ) );
        }
        System.out.println();
    }
}
